<?php

class CMS_Controller_Action_Admin extends CMS_Controller_Action_Auth {

    public function init() {

        parent::init();


        if (!MD_User::hasIdentity()) {
            $this->_redirect("/index");
        } else {
            $this->view->name = MD_User::getName();
            $this->view->last_name = MD_User::getSurname();
            $data = MD_User::getCategory();
            $modules = new CMS_Models_Module();
            $this->view->modules = $modules->getRootModules($data);
            $this->view->rootModule = $this->getActualRootModule();
            $request = $this->_request->getParams();
            $this->view->subModules = $modules->getSubModules($this->view->rootModule[0]['id'], $data);
            $this->view->actualModule = $request['controller'];
            if (!$modules->categoryHasPermision($data, $this->view->actualModule)) {
                $permisions = $modules->getModulesByCategory($data);
                if (count($permisions) > 0) {
                    $this->_redirect("/admin/" . $permisions[0]['action']);
                } else {
                    $this->_redirect("/admin/error");
                }
            }
        }
    }

    private function getActualRootModule() {
        $request = $this->_request->getParams();
        $m = new CMS_Models_Module();
        return $m->getActualRootModule($request['controller']);
    }

}